package com.laker.admin.module.ext.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laker.admin.framework.aop.metrics.Metrics;
import com.laker.admin.framework.aop.trace.LakerTrace;
import com.laker.admin.framework.model.PageResponse;
import com.laker.admin.framework.model.Response;
import com.laker.admin.module.ext.entity.ExtDo;
import com.laker.admin.module.ext.service.IExtDoService;
import io.swagger.annotations.ApiOperation;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.entity.WorkItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author laker
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/ext/do")
@Metrics
@LakerTrace
public class ExtDoController {
    @Autowired
    IExtDoService extDoService;

    @GetMapping
    @ApiOperation(value = "分页查询")
    public PageResponse pageAll(@RequestParam(required = false, defaultValue = "1") long page,
                                @RequestParam(required = false, defaultValue = "10") long limit,
                                String keyWord) {
        Page roadPage = new Page<>(page, limit);
        LambdaQueryWrapper<ExtDo> queryWrapper = new QueryWrapper().lambda();

        queryWrapper.eq(ExtDo::getDelFlag, 0).and(StrUtil.isNotBlank(keyWord), i -> i.
                like(StrUtil.isNotBlank(keyWord), ExtDo::getDoTitle, keyWord)
                .or().like(StrUtil.isNotBlank(keyWord), ExtDo::getDoContent, keyWord));

        Page pageList = extDoService.page(roadPage, queryWrapper);
        return PageResponse.ok(pageList.getRecords(), pageList.getTotal());
    }

    @PostMapping
    @ApiOperation(value = "新增或者更新")
    public Response saveOrUpdate(@RequestBody ExtDo param) {
        return Response.ok(extDoService.saveOrUpdate(param));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public Response get(@PathVariable Long id) {
        return Response.ok(extDoService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public Response delete(@PathVariable Long id) {
        return Response.ok(extDoService.removeById(id));
    }

    /**
     * 查询待办任务列表
     */
    @GetMapping("/todoList")
    @ApiOperation(value = "待办任务列表", tags = "任务")
    public PageResponse todoList() {
        Page roadPage = new Page<>();
        LambdaQueryWrapper<ExtDo> queryWrapper = new QueryWrapper().lambda();
        Page pageList = extDoService.getTodoList(roadPage, queryWrapper);
        return PageResponse.ok(pageList.getRecords(), pageList.getTotal());
    }

    /**
     * 查询待办任务列表
     */
    @GetMapping("/doneList")
    @ApiOperation(value = "已办任务列表", tags = "任务")
    public PageResponse doneList() {
        Page roadPage = new Page<>();
        LambdaQueryWrapper<ExtDo> queryWrapper = new QueryWrapper().lambda();
        Page pageList = extDoService.getDoneList(roadPage, queryWrapper);
        return PageResponse.ok(pageList.getRecords(), pageList.getTotal());
    }
}
